#!/bin/bash

echo "🚀 =============== SSL握手失败修复脚本 ==============="
echo ""

echo "📍 步骤1：清理Gradle缓存"
echo "正在清理Gradle缓存目录..."
if [ -d "$HOME/.gradle" ]; then
    echo "删除Gradle用户目录缓存..."
    rm -rf "$HOME/.gradle/caches" 2>/dev/null
    rm -rf "$HOME/.gradle/wrapper" 2>/dev/null
    echo "✅ Gradle用户缓存已清理"
else
    echo "⚠️ Gradle用户目录不存在，跳过"
fi

echo ""
echo "📍 步骤2：清理项目缓存"
cd "$(dirname "$0")"

if [ -d "android/.gradle" ]; then
    echo "删除项目Gradle缓存..."
    rm -rf "android/.gradle"
    echo "✅ 项目Gradle缓存已清理"
fi

if [ -d "build" ]; then
    echo "删除构建缓存..."
    rm -rf "build"
    echo "✅ 构建缓存已清理"
fi

if [ -d "android/build" ]; then
    echo "删除Android构建缓存..."
    rm -rf "android/build"
    echo "✅ Android构建缓存已清理"
fi

if [ -d ".dart_tool" ]; then
    echo "删除Dart工具缓存..."
    rm -rf ".dart_tool"
    echo "✅ Dart工具缓存已清理"
fi

echo ""
echo "📍 步骤3：清理Flutter缓存"
echo "正在运行Flutter清理命令..."
flutter clean
echo "✅ Flutter缓存已清理"

echo ""
echo "📍 步骤4：重新获取依赖"
echo "正在重新获取Flutter依赖..."
flutter pub get
echo "✅ Flutter依赖已重新获取"

echo ""
echo "🎯 =============== 项目修复完成 ==============="
echo "📋 已完成以下修复操作："
echo "   ✅ 修改gradle-wrapper.properties使用腾讯云镜像"
echo "   ✅ 强化SSL和网络配置"
echo "   ✅ 修复Java home路径（跨平台兼容）"
echo "   ✅ 清理所有缓存"
echo "   ✅ 重新获取依赖"
echo ""
echo "💡 现在可以尝试运行项目："
echo "   flutter run"
echo ""
echo "🔧 如果还有问题，请尝试以下替代方案："
echo "   1. 切换网络环境（如手机热点）"
echo "   2. 检查防火墙和代理设置"
echo "   3. 使用VPN或更换DNS"
echo ""
read -p "按Enter键退出..." input
